package cn.edu.neusoft.yyd208.foodorder.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import cn.edu.neusoft.yyd208.foodorder.R;
import cn.edu.neusoft.yyd208.foodorder.bean.LoginBean;
import cn.edu.neusoft.yyd208.foodorder.listener.TListener;
import cn.edu.neusoft.yyd208.foodorder.model.LoginModel;

public class LoginActivity extends BaseActivity {
    EditText et_user,et_password;
    TextView tv_userid;
    Button btn_login,btn_register;
    String username,userpass;
    CheckBox checkBox;
    Boolean remember;
    TListener<LoginBean> tListener=new TListener<LoginBean>() {
    @Override
    public void onResponse(LoginBean loginbean) {
        if(loginbean.getUserid().equals("0")){
            showToast("登录失败");
        }
        else{

            showToast("登录成功");
            SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
            SharedPreferences.Editor editor=sp.edit();
            editor.putString("userid",loginbean.getUserid());
            editor.commit();
            saveUser();
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivityForResult(intent,1);
        }
    }

    @Override
    public void onFail(String msg) {
        showToast("登录失败");
    }
};

    @Override
    void initView() {
        setLayout_file(R.layout.activity_login);
        et_user=(EditText)findViewById(R.id.et_user);
        et_password=(EditText)findViewById(R.id.et_password);
        tv_userid=(TextView) findViewById(R.id.tv_userid);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_register=(Button)findViewById(R.id.btn_register);
        checkBox=(CheckBox)findViewById(R.id.checkBox);
        username=getUser_name();
        userpass=getUser_userpass();
        remember=getUser_remember();
        if(remember==true){
            et_user.setText(username);
            et_password.setText(userpass);
        }
    }

    @Override
    void initEvent() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_user.getText().toString().trim().equals("")||et_password.getText().toString().trim().equals("")){
                    showToast("不能为空");
                }
                else {
                    login();
                    saveUser();

//                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    //把用户名和密码放到服务器上验证
                }
            }
        });
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });

    }
protected void onActivityResult(int requestCode,int resultCode,Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    switch (requestCode){
        case 1:
            if (resultCode==RESULT_OK){
                String username2=data.getStringExtra("user");
                et_user.setText(username2);
            }
    }
}
    @Override
    void initData() {

    }

    void login(){
        username=et_user.getText().toString();
        userpass=et_password.getText().toString();
        LoginModel loginModel=new LoginModel();
        loginModel.getLoginMessage(username,userpass,tListener);

    }
    public void saveUser(){
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
       // editor.putString("userid",loginbean.getUserid());//tv_userid.getText().toString()
        editor.putString("username",et_user.getText().toString());
        editor.putString("userpass",et_password.getText().toString());
        editor.putBoolean("remember",checkBox.isChecked());
        editor.commit();
    }

}
